<?php
class App_Track_Build
{
    public function __construct ($trackId = NULL)
    {
        $this->_trackId = $trackId;
    }

    // 创建基本线路
    // STEP 1
    public function createByBasic ($uid, $country, $city,$cityId,
        $supportCar, $supportBus, $srv70s, $srv80s, $srv90s, $srv00s, 
        $minPersons, $maxPersons, $days)
    {
        $row = array(

            'city'       => $city,
            'cityId'    =>$cityId,
            'country'    => $country,

            'uid'        => $uid,
            'created'    => DB::expr('unix_timestamp(now())'),
            'changed'    => DB::expr('unix_timestamp(now())'),
            'status'     => 'INVALID',

            'support70s' => (int) $srv70s,
            'support80s' => (int) $srv80s,
            'support90s' => (int) $srv90s,
            'support00s' => (int) $srv00s,

            'serviceMinPersons' => (int) $minPersons,
            'serviceMaxPersons' => (int) $maxPersons,

            'trafficSupportCar' => (int) $supportCar,
            'trafficSupportBus' => (int) $supportBus,

            'days' => $days,
        );

        list($insert_id, $rows) = DB::insert('mod_track')
            ->columns (array_keys($row))
            ->values(array_values($row))
            ->execute();

        return $insert_id;
    }

    // 更新基本线路
    // STEP 1
    public function updateBasic ($trackId, $uid, $country, $city,$cityId, 
        $supportCar, $supportBus, $srv70s, $srv80s, $srv90s, $srv00s, 
        $minPersons, $maxPersons, $days)
    {
        $row = array(

            'city'       => $city,
            'cityId'    =>$cityId,
            'country'    => $country,

            'changed'    => DB::expr('unix_timestamp(now())'),

            'support70s' => (int) $srv70s,
            'support80s' => (int) $srv80s,
            'support90s' => (int) $srv90s,
            'support00s' => (int) $srv00s,

            'serviceMinPersons' => (int) $minPersons,
            'serviceMaxPersons' => (int) $maxPersons,

            'trafficSupportCar' => (int) $supportCar,
            'trafficSupportBus' => (int) $supportBus,

            'days' => $days,
        );

        $rows = DB::update('mod_track')
            ->set($row)
            ->where('id', '=', $trackId)
            ->execute();

        return $trackId;
    }

 

    public function updateDays ($trackId, $name, $summary, $thumbUrl, $bannerUrl, $scenics = array())
    {
        $row = array(
            'name'    => $name,
            'summary' => $summary,
            'changed' => DB::expr('unix_timestamp(now())'),
        );

        if (! empty($thumbUrl))
            $row['thumb'] = $thumbUrl;

        if (! empty($bannerUrl))
            $row['bannerUrl'] = $bannerUrl;

        $rows = DB::update('mod_track')
            ->set($row)
            ->where('id', '=', $trackId)
            ->execute();

        if (! empty($scenics))
        {
            DB::delete('mod_track_days')
                ->where('trackId', '=', $trackId)
                ->execute();

            $query = DB::insert('mod_track_days')
                ->columns (array('trackId', 'dayNo', 'scenicId', 'timeStart', 'timeStop', 'desc'));
            foreach ($scenics as $item)
            {
                
                $query->values (array($trackId, $item['dayNo'], $item['scenicId'], $item['startTime'], $item['stopTime'], $item['desc']));
            }

            $query->execute();
        }

        return TRUE;
    }

    public function updateTimes ($trackId, $startDay, $stopDay, $freeTimeDays)
    {
        $row = array(
            'startDay'=> $startDay,
            'stopDay' => $stopDay,
            'freeTimeDays' => $freeTimeDays,
            'changed' => DB::expr('unix_timestamp(now())'),
        );

        $rows = DB::update('mod_track')
            ->set($row)
            ->where('id', '=', $trackId)
            ->execute();

        return $rows > 0;
    }

    public function updatePrice($trackId, $priceTraffic, $priceTicket, $priceFood, $priceService, $priceOther, $descFood, $descTraffic, $descTicket, $descOther)
    {
        $track = new App_Track($trackId);
        $days  = $track->getDaysService();

        $row = array(

            'priceTraffic' => (int) ($priceTraffic * 100),
            'descTraffic'  => $descTraffic,

            'priceTicket'  => (int) ($priceTicket * 100),
            'descTicket'   => $descTicket,

            'priceFood'    => (int) ($priceFood * 100),
            'descFood'     => $descFood,

            'priceOther'   => (int) ($priceOther * 100),
            'descOther'    => $descOther,

            'priceService' => (int) ($priceService * 100),

            'priceTotal'   => (int)(($priceService + $priceTraffic + $priceTicket + $priceFood + $priceOther) * 100),

            'changed' => DB::expr('unix_timestamp(now())'),
        );

        $rows = DB::update('mod_track')
            ->set($row)
            ->where('id', '=', $trackId)
            ->execute();

        return $rows > 0;
    }

    public function updateBank($trackId, $accountType, $accountName, $accountUserName)
    {
        $row = array(
            'accountType'     => $accountType,
            'accountName'     => $accountName,
            'accountUserName' => $accountUserName,

            'status'  => 'VALID',
            'changed' => DB::expr('unix_timestamp(now())'),
        );

        $rows = DB::update('mod_track')
            ->set($row)
            ->where('id', '=', $trackId)
            ->execute();

        return $rows > 0;
    }



}
